#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#include "./crc.h"



void test_crc(uint32_t init, uint8_t *pbuf, uint32_t size)
{
    uint32_t ret;

    ret = crc32_fsl(init, pbuf, sizeof(size));
    printf("crc test_buff 0x%08X ret = 0x%08X\r\n", init, ret);
}

int test_file(void)
{
    FILE *fp;
    int file_size;
    char *file_buff;

    fp = fopen("./test", "rw+");
    if (fp == NULL)
    {
        printf("open file fail\r\n");
        return -1;
    }

    /* get file size */
    fseek(fp, 0, SEEK_END);
    file_size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    printf("file size = %d\r\n", file_size);

    /* malloc the file buff */
    file_buff = malloc(file_size + 8);
    if (file_buff == NULL)
    {
        printf("file buff malloc fail\r\n");
        return -1;
    }
    memset(file_buff, 0x00, file_size);

    /* read file data */
    fread(file_buff, 1, file_size+1, fp);
    printf("file data = %s\r\n", file_buff);

    printf("file buff = \r\n");
    for (size_t i = 0; i < file_size+8; i++)
    {
        printf("%02X ", file_buff[i]);
    }
    printf("\r\n");
    
    fclose(fp);

    return 0;
}


int main(int argc, char **argv)
{
    uint8_t test_buff[256];
    uint32_t crc_init = 0;
    uint32_t ret;

    for (size_t i = 0; i < sizeof(test_buff); i++)
    {
        test_buff[i] = (uint8_t)i;
    }

    test_crc(0x00000000, test_buff, sizeof(test_buff));
    test_crc(0xFFFFFFFF, test_buff, sizeof(test_buff));
    test_crc(0x55555555, test_buff, sizeof(test_buff));
    test_crc(0xAAAAAAAA, test_buff, sizeof(test_buff));

    test_file();

    return 0;
}




